package it.blueocean.acanto.taglibrary.jsp.taglib.tags;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.container.ContainerStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanPropertyNotFoundException;

public class StateBeansManager extends HashMap
{
	private static final long serialVersionUID = 8967454860235L;

	public static final String SESSION_ATTRIBUTE_NAME = "state_beans_manager";

	public static final String SESSION_SCOPE = "session";

	public static final String PAGE_SCOPE = "page";

	public static final String REQUEST_SCOPE = "request";

	public static StateBeansManager getInstance(HttpSession paramHttpSession)
	{
		StateBeansManager localStateBeansManager = (StateBeansManager) paramHttpSession
				.getAttribute("state_beans_manager");
		if(localStateBeansManager == null)
		{
			localStateBeansManager = new StateBeansManager();
			paramHttpSession.setAttribute("state_beans_manager", localStateBeansManager);
		}
		return localStateBeansManager;
	}

	public void setStateBean(String paramString, AbstractComponentStateBean paramAbstractComponentStateBean)
	{
		put(paramString, paramAbstractComponentStateBean);
	}

	public void setStateBeanProperty(StateBeanPropertyLocator paramStateBeanPropertyLocator, String[] paramArrayOfString)
			throws ComponentStateBeanNotFoundException, ComponentStateBeanPropertyNotFoundException,
			InvocationTargetException
	{
		AbstractComponentStateBean localAbstractComponentStateBean = getStateBean(paramStateBeanPropertyLocator);
		localAbstractComponentStateBean
				.setProperty(paramStateBeanPropertyLocator.getPropertyName(), paramArrayOfString);
	}

	public Object getStateBeanProperty(StateBeanPropertyLocator paramStateBeanPropertyLocator)
			throws ComponentStateBeanNotFoundException, ComponentStateBeanPropertyNotFoundException
	{
		AbstractComponentStateBean localAbstractComponentStateBean = getStateBean(paramStateBeanPropertyLocator);
		return localAbstractComponentStateBean.getProperty(paramStateBeanPropertyLocator.getPropertyName());
	}

	public AbstractComponentStateBean getStateBean(String paramString) throws ComponentStateBeanNotFoundException
	{
		StateBeanLocator localStateBeanLocator = new StateBeanLocator(paramString);
		return getStateBean(localStateBeanLocator);
	}

	public AbstractComponentStateBean getStateBean(StateBeanLocator paramStateBeanLocator)
			throws ComponentStateBeanNotFoundException
	{
		return getStateBean(paramStateBeanLocator, 0);
	}

	private AbstractComponentStateBean getStateBean(StateBeanLocator paramStateBeanLocator, int paramInt)
			throws ComponentStateBeanNotFoundException
	{
		if(!(containsKey(paramStateBeanLocator.getComponentPath()[paramInt])))
			throw new ComponentStateBeanNotFoundException(paramStateBeanLocator.toString());
		AbstractComponentStateBean localAbstractComponentStateBean = (AbstractComponentStateBean) get(paramStateBeanLocator
				.getComponentPath()[paramInt]);
		if((paramInt < paramStateBeanLocator.getComponentPath().length - 2)
				&& (localAbstractComponentStateBean instanceof ContainerStateBean))
			return ((ContainerStateBean) localAbstractComponentStateBean).getComponents().getStateBean(
					paramStateBeanLocator, paramInt + 2);
		return localAbstractComponentStateBean;
	}

	public AbstractComponentStateBean getStateBean(StateBeanPropertyLocator paramStateBeanPropertyLocator)
			throws ComponentStateBeanNotFoundException
	{
		return getStateBean(paramStateBeanPropertyLocator.getBeanLocator());
	}

}
